
<ol class="breadcrumb">
	<li><a href="/users/index"><i class="fa fa-home"></i>&nbsp;&nbsp;{{__('Dashboard')}}</a></li>
	<li class="active">{{__('My Orders')}}</li>
</ol>
<hr>
<script src="/js/clipboard.min.js"></script>
<div class="orderinfo">
	<style>
		.table > tbody > tr > td{padding:10px;}
		.table > tbody + tbody {
			border-top: none;
		}
		{{if $is_mobile}}
		.item-price,.item-sum,.carts-head{display:none;}.item-nums{width:60px;text-align: center;}
		body,#maincontent{background:#f3f3f3;}
		.table{margin-bottom: 0px;}
		.nav > li > a {  padding: 8px;}
		{{else}}
		.order-carts{padding:10px;}.order-head{background:#f4f4f4;}
		{{/if}}
		.order-item{background: #FFF;border:1px solid #eee;margin-bottom: 30px;border-radius: 5px;}
		.order-head{line-height:32px;padding:5px 10px;border-bottom:1px solid #eee;}
		.order-status{padding:10px;border-top:1px solid #eee;}
		.order-time{color:#999;margin-left:60px;}
		label{font-weight: normal;margin-bottom: 0px;}.well{padding:10px;}
		.mb10{margin-bottom: 10px}
	</style>
	<div>
	{{if $is_business}}
		<div class="form-inline" class="mb10">
			<form data-noajax="true" methed="post">
				{{if $_REQUEST['model']}}
				<input type="hidden" name="model" value="{{$_REQUEST['model']}}">
				{{/if}}
				{{if $_REQUEST['data_id']}}
				<input type="hidden" name="data_id" value="{{$_REQUEST['data_id']}}">
				{{/if}}
				<input type="hidden" name="type" value="shop">
				<input type="hidden" name="shop_id" value="{{$current_user_id}}">

				<input type="text" value="{{$_REQUEST['word']}}" class="form-control" name="word" placeholder="输入昵称或手机号搜索" value="">
				<input  class="btn btn-primary" type="submit" value="搜索">
				<a  class="btn btn-default" style="margin-right: 0;" href="/orders/mine?type=shop&model={{$_REQUEST['model']}}&data_id={{$_REQUEST['data_id']}}">取消</a>
			</form>
		</div>


		{{if $total_orders}}
		<p>订单统计：订单数 <strong>{{$total_orders}}</strong>，总金额 <strong>{{$total_fees}}</strong>元，快递{{$total_orders-$pickup_nums}}，自提{{$pickup_nums}}</p>
		<p>产品统计：{{loop $cart_nums $c}} {{usubstr($c['name'],0,4,'')}}(<strong>{{$c['num']}}</strong>)&nbsp; {{/loop}}</p>
		{{/if}}

		<ul class="nav nav-tabs" style="margin:10px 0;">
			<?php  $filter = $_REQUEST; unset($filter['pickup_address']);unset($filter['id']);unset($filter['status']);unset($filter['ship_type']);unset($filter['word']); ?>
			<li {{if !isset($_REQUEST['status'])}}class="active"{{/if}}><?php echo $this->Html->link('全部', array('controller' => 'orders','action'=>'mine','plugin' => null ,'?'=>$filter)); ?></li>

			{{if in_array($shop_id,$reserve_shopids)}}
				<li {{if $_REQUEST['status']==-10}}class="active"{{/if}}><?php echo $this->Html->link('待确认'.($status_nums[-10]>0?'<span class="badge" style="display: inline;">'.$status_nums[-10].'</span>':''), array('controller' => 'orders','action'=>'mine','plugin' => null ,'?'=>array_merge($filter,array('status'=>-10))),array('escape'=>false)); ?></li>
				<li {{if isset($_REQUEST['status']) && $_REQUEST['status']==0}}class="active"{{/if}}><?php echo $this->Html->link('待付款'.($status_nums[0]>0?'<span class="badge" style="display: inline;">'.$status_nums[0].'</span>':''), array('controller' => 'orders','action'=>'mine','plugin' => null,'?'=>array_merge($filter,array('status'=>0)) ,),array('escape'=>false)); ?></li>
			{{/if}}

			<li {{if $_REQUEST['status']==1}}class="active"{{/if}}><?php echo $this->Html->link('待发货'.($status_nums[1]>0?'<span class="badge" style="display: inline;">'.$status_nums[1].'</span>':''), array('controller' => 'orders','action'=>'mine','plugin' => null ,'?'=>array_merge($filter,array('status'=>1))),array('escape'=>false)); ?></li>
			<li {{if $_REQUEST['status']==2}}class="active"{{/if}}><?php echo $this->Html->link('已发货'.($status_nums[2]>0?'<span class="badge" style="display: inline;">'.$status_nums[2].'</span>':''), array('controller' => 'orders','action'=>'mine','plugin' => null,'?'=>array_merge($filter,array('status'=>2))),array('escape'=>false)); ?></li>
		</ul>


		<ul class="nav nav-tabs" style="margin:10px 0;margin-left:20px;">
			<?php  $filter = $_REQUEST;unset($filter['pickup_address']); unset($filter['id']);unset($filter['word']); ?>

			<li {{if isset($_REQUEST['ship_type']) && $_REQUEST['ship_type']=='express'}}class="active"{{/if}}><?php echo $this->Html->link('快递', array('controller' => 'orders','action'=>'mine','plugin' => null ,'?'=>array_merge($filter,array('ship_type'=>'express'))),array('escape'=>false)); ?></li>
			<li {{if $_REQUEST['ship_type']==200}}class="active"{{/if}}><?php echo $this->Html->link('自提', array('controller' => 'orders','action'=>'mine','plugin' => null,'?'=>array_merge($filter,array('ship_type'=>200))),array('escape'=>false)); ?></li>
		</ul>
		{{if !empty($pickups) && $_REQUEST['ship_type'] != 'express'}}
			<ul class="nav nav-tabs" style="margin:10px 0;margin-left:20px;">
				<?php  $filter = $_REQUEST;unset($filter['pickup_address']); unset($filter['id']);unset($filter['word']); ?>

				{{loop $pickups $adr}}
				<li {{if isset($_REQUEST['pickup_address']) && $_REQUEST['pickup_address']==$adr}}class="active"{{/if}}><?php echo $this->Html->link($adr, array('controller' => 'orders','action'=>'mine','plugin' => null ,'?'=>array_merge($filter,array('pickup_address'=>$adr))),array('escape'=>false)); ?></li>
				{{/loop}}
			</ul>

		<!-- <p class="mb10">
			<label style="padding-right:30px"><input type="radio" name="orderfilters" value="all" checked="checked">全部</label>
			<label style="padding-right:30px"><input type="radio" name="orderfilters" value="express">快递</label>
			<label style="padding-right:30px"><input type="radio" name="orderfilters" value="pickup">自提 {{$pickup_nums}}</label>
		</p>
		<p id="addresslist" style="display:none;margin: 10px 10px 10px 20px; padding: 10px;border: 1px dashed #ddd;"> <label style="padding-right:10px"><input type="radio" name="pickupfilters" value="all">全部</label> {{loop $pickups $adr}} <label style="padding-right:10px"><input type="radio" name="pickupfilters" value="{{$adr}}">{{$adr}} ({{$pick_item_nums[$adr]}})</label> {{/loop}}</p>
		-->
		{{/if}}
	{{/if}}
		{{loop $orders $order}}
		<div data-pickup="{{$order['Order']['pickup_address']}}" class="order-item {{if $order['Order']['status']==1}} paid{{elseif  $order['Order']['status']==2}} shipped{{/if}} {{if $order['Order']['ship_type']==200}} ship_pickup{{else}} ship_express{{/if}}">
			<div class="order-head">
				{{if $is_business &&  $order['Order']['status']==1 }}
				<input id="order-ck-{{$order['Order']['id']}}" type="checkbox" name="orders[]" value="{{$order['Order']['id']}}">
				{{/if}}
				{{if $is_business}}
				<label for="order-ck-{{$order['Order']['id']}}">{{$order['Order']['nickname']}} </label>&nbsp;<small style="font-size: 12px;">订单编号：{{$order['Order']['id']}} </small>
				{{else}}
				订单编号：{{$order['Order']['id']}} &nbsp;
				{{/if}}
				<small class="pull-right order-time">
					{{if $order['Order']['ship_type']==200}}自提{{else}}快递{{/if}}

					{{if $order['Order']['model']=='ReserveTrain'}}<span style="margin:0 10px;">火车票</span>{{/if}}

					{{if $order['Order']['status']==0}} <font  id="order-stat-txt-{{$order['Order']['id']}}" color="red">待付款</font>
					{{elseif $order['Order']['status']==1}} 
						{{if $is_business}}
						<font  id="order-stat-txt-{{$order['Order']['id']}}" color="red">待发货</font>
						{{else}}
							<font  id="order-stat-txt-{{$order['Order']['id']}}" color="green">已支付</font>
						{{/if}}
					{{elseif $order['Order']['status']==2}} <font  id="order-stat-txt-{{$order['Order']['id']}}" color="green">已发货</font>
					{{elseif $order['Order']['status']==3}} <font  id="order-stat-txt-{{$order['Order']['id']}}" color="green">已收货</font>
					{{elseif $order['Order']['status']==-1}} <font  id="order-stat-txt-{{$order['Order']['id']}}" color="green">已退款</font>
					{{elseif $order['Order']['status']==-10}} <font  id="order-stat-txt-{{$order['Order']['id']}}" color="green">待确认</font>
					{{elseif $order['Order']['status']==-11}} <font  id="order-stat-txt-{{$order['Order']['id']}}" color="green">已无货</font>
					{{else}}
					{{/if}}
				</small>
			</div>
			<?php $order_goods_txt = "\r\n";$is_reserve = false;
				if( strpos($order['Order']['model'],'Reserve')!==false ) { $is_reserve = true;}
			 ?>

			{{if !empty($order_carts[$order['Order']['id']])}}
			<div class="order-carts">
				{{if $is_mobile}}
				<div style="padding:10px 10px 0">
					产品：
					{{loop $order_carts[$order['Order']['id']] $cart}}
					
						{{if is_array($cart['Cart']['serialize_info'])}}
							<p class="mb10" style="word-break: break-all;"> <strong>{{$cart['Cart']['name']}}</strong> <small>({{$cart['Cart']['num']}}份)</small>&nbsp;&nbsp;&nbsp;
								{{loop $cart['Cart']['serialize_info'] $k $v}}
									{{$k}}：{{$v}}&nbsp;&nbsp;&nbsp;
								{{/loop}}
								{{if $is_reserve || !$is_business}}价格：
								<span class="product-price"><small>{{format_price($cart['Cart']['price']*$cart['Cart']['num'])}}</small></span>
								{{/if}}
							</p>
						{{else}}
							{{if !$is_business}} <p class="mb10"> {{/if}}
							<strong>{{$cart['Cart']['name']}}</strong> <small>({{$cart['Cart']['num']}}份)</small>&nbsp;&nbsp;&nbsp;
							{{if $is_reserve || !$is_business}}价格：
								<span class="product-price"><small>{{format_price($cart['Cart']['price']*$cart['Cart']['num'])}}</small></span>
								{{/if}}
							{{if !$is_business}} </p> {{/if}}
						{{/if}}
					{{/loop}}

				</div>
				{{else}}
				<table  class="table table-noborder" style="width:100% !important;margin-top:5px;margin-bottom:0px;">
					<tr class="carts-head"><th width="50%">商品名称</th><th class="item-price">价格</th><th width="60px" class="item-nums">数量</th><th class="item-sum">合计</th></tr>
					<tbody>
					{{loop $order_carts[$order['Order']['id']] $cart}}
					<?php $order_goods_txt .= $cart['Cart']['name'].' '.$cart['Cart']['num']."\r\n"; ?>
					<tr><td>
						{{$cart['Cart']['name']}}
						{{if is_array($cart['Cart']['serialize_info'])}}
							<p> <small>
								{{loop $cart['Cart']['serialize_info'] $k $v}}
									{{$k}} : <span style="margin-right:15px;">{{$v}}</span>
								{{/loop}}</small>
							</p>
						{{/if}}
					</td>
						<td class="item-price">￥{{$cart['Cart']['price']}}</td>
						<td  width="60px" class="item-nums">{{$cart['Cart']['num']}}</td>
						<td class="item-sum">￥{{$cart['Cart']['price']*$cart['Cart']['num']}}</td>
					</tr>
					{{/loop}}
					</tbody>
				</table>
				{{/if}}
			</div>
			{{/if}}
			<div class="order-status" style="border-top:0 none;">

				{{if is_array($order['Order']['serialize_info'])}}
					
						{{loop $order['Order']['serialize_info'] $k $v}}
							<p> <small>{{$k}}</small> : <span style="margin-right:15px;">{{$v}}</span></p>
						{{/loop}}
					
				{{/if}}

				{{if $order['Order']['consignee_name']}}
				<p class="mb10">联系人：{{$order['Order']['consignee_name']}}，{{$order['Order']['consignee_province']}}{{$order['Order']['consignee_city']}}{{$order['Order']['consignee_area']}}{{$order['Order']['consignee_address']}}，{{$order['Order']['consignee_mobilephone']}}</p>
				{{/if}}

				{{if $order['Order']['ship_fee']}}
					<p class="mb10">快递费：<span class="product-price"><small>{{format_price($order['Order']['ship_fee'])}}</small></span> </p>
				{{/if}}
				<p class="mb10">总价：<span class="product-price">{{format_price($order['Order']['total_price'])}}</span> <small class="order-time">{{substr($order['Order']['created'],5,11)}}</small></p>


				{{if $order['Order']['ship_code']}}
				<p class="mb10">快递: {{$ship_type[$order['Order']['ship_type']]}} ,&nbsp;&nbsp; 快递单号：{{$order['Order']['ship_code']}}</p>
				{{/if}}
				{{if $order['Order']['ship_type']==200}}
				<p class="mb10">自提：{{$order['Order']['pickup_address']}}</p>
				{{/if}}

				{{if $is_business}}
				<div class="form-inline" id="order-status-{{$order['Order']['id']}}">

					{{if $order['Order']['status']==0}}
						<span style="color:red;">订单待付款</span>
					{{elseif $order['Order']['status']==-10}}
					<p class="mb10">
						<a target="_blank" href="/orders/edit/{{$order['Order']['id']}}" class="btn btn-primary"><i class="fa fa-edit"></i> 确认座位</a>
						<a onclick="if(confirm('是否确认各项价格填写无误')) order_status({{$order['Order']['id']}},0);" href="javascript:void(0);" class="btn btn-primary"><i class="fa fa-bell"></i> 提醒支付</a>
					</p>
					<p>
						<a onclick="if(confirm('是否确认无货')) order_status({{$order['Order']['id']}},-11);" href="javascript:void(0);" class="btn btn-primary"><i class="fa fa-ban"></i> 标记无票</a>
					</p>
					{{elseif $order['Order']['status']==-11}}
						<span style="color:red;">订单确认无货</span>
					{{elseif $order['Order']['status']==1}}
					<div id="ship-{{$order['Order']['id']}}" class="well">
						{{if $order['Order']['ship_type']==200}}
						<p class="mb10">
							<input id="ship-type-{{$order['Order']['id']}}" type="hidden" value="200">
							<input id="ship-code-{{$order['Order']['id']}}" type="hidden" value="0">
							<button type="button"  onclick="ship_order({{$order['Order']['id']}});" class="btn btn-ship btn-primary">发货</button>
							<button class="btn-clip btn btn-primary" data-clipboard-text="收件人信息：{{$order['Order']['consignee_name']}}，{{$order['Order']['consignee_mobilephone']}}，{{$order['Order']['consignee_province']}}{{$order['Order']['consignee_city']}}{{$order['Order']['consignee_area']}}{{$order['Order']['consignee_address']}}
				{{$order_goods_txt}}">复制订单</button>
						</p>
						{{else}}
						<p class="mb10 btn-ship"><select class="form-control" id="ship-type-{{$order['Order']['id']}}">
							<option value="">快递类型</option>
							{{loop $ship_type $id $type}}
							<option value="{{$id}}">{{$type}}</option>
							{{/loop}}
						</select>
						<input  class="form-control" type="text" style="width:120px;" id="ship-code-{{$order['Order']['id']}}" placeholder="快递单号">
						<button type="button"  onclick="ship_order({{$order['Order']['id']}});" class="btn btn-primary">发货</button>
						</p>
						<p><button class="btn-clip btn btn-primary" data-clipboard-text="收件人信息：{{$order['Order']['consignee_name']}}，{{$order['Order']['consignee_mobilephone']}}，{{$order['Order']['consignee_province']}}{{$order['Order']['consignee_city']}}{{$order['Order']['consignee_area']}}{{$order['Order']['consignee_address']}}
				{{$order_goods_txt}}">
							复制订单</button></p>
						{{/if}}

					</div>
					{{elseif $order['Order']['status']==2}}
						<div id="ship-{{$order['Order']['id']}}" class="well">
						{{if empty($order['Order']['ship_code'])}}
							{{if $order['Order']['ship_type']!=200}}
							<p class="mb10 btn-ship"><select class="form-control" id="ship-type-{{$order['Order']['id']}}">
								<option value="">快递类型</option>
								{{loop $ship_type $id $type}}
								<option value="{{$id}}">{{$type}}</option>
								{{/loop}}
							</select>
							<input  class="form-control" type="text" style="width:120px;" id="ship-code-{{$order['Order']['id']}}" placeholder="快递单号">
							<button type="button"  onclick="ship_order({{$order['Order']['id']}});" class="btn btn-primary">录入</button>
							</p>
							{{/if}}
						{{/if}}
							<p><button class="btn-clip btn btn-primary btn-sm" data-clipboard-text="收件人信息：{{$order['Order']['consignee_name']}}，{{$order['Order']['consignee_mobilephone']}}，{{$order['Order']['consignee_province']}}{{$order['Order']['consignee_city']}}{{$order['Order']['consignee_area']}}{{$order['Order']['consignee_address']}}
				{{$order_goods_txt}}">
								复制订单</button></p>

						</div>
					{{/if}}
				</div>
				{{else}}
				{{if $order['Order']['status']==0}}
				<a href="{{$this->Html->url('/orders/pay/'.$order['Order']['id'])}}" class="btn btn-primary">立即付款</a>
				{{/if}}
				{{/if}}

				{{if $order['Order']['remark']}}
					<p>备注：{{$order['Order']['remark']}}</p>
				{{/if}}

				{{if !$is_mobile}}
				<p><a target="_blank" href="{{$this->Html->url('/orders/info/'.$order['Order']['id'])}}">查看详情</a></p>
				{{/if}}

			</div></div>
		{{/loop}}

	</div>
</div>
{{if $is_business && (!isset($_REQUEST['status']) || $_REQUEST['status']>0)}}

<?php  $code = rawurlencode(authcode($shop_id.'_'.time(),'ENCODE')); ?>
<div {{if $is_mobile}}class="navbar-fixed-bottom"{{/if}} style="padding-top: 10px;" >

	<a class="btn btn-default" style="padding:8px 15px" href="javascript:;"><label><input onclick="checkAll(this,'.order-item');" type="checkbox" id="select-all">全选</label></a><a class="btn btn-primary" style="padding:8px 20px" onclick="return batch_ship(this);" href="javascript:;"><i class="fa fa-paper-plane" ></i> 批量发货</a>

	<a class="btn btn-primary" style="padding:8px 20px;margin-right: 0;" target="_blank" href="/orders/export/{{$model}}-{{$data_id}}/{{$code}}?model={{$model}}&data_id={{$data_id}}&code={{$code}}"><i class="fa fa-download"></i> 导出Excel</a>
</div>
{{/if}}

<script>


    $('[name=orderfilters]').click(function(){
        $('.order-item').hide(); $('#addresslist').hide();
        if( $('[name=orderfilters]:checked').val() == 'all' ){
            $('.order-item').show('normal');
        }
        else if( $('[name=orderfilters]:checked').val() == 'express' ){
            $('.order-item.ship_express').show('normal');
        }
        else{
            //自提方式：
            $('.order-item.ship_pickup').show('normal');
            $('#addresslist').show();
        }
    });

    $('[name=pickupfilters]').click(function(){
        if( $('[name=pickupfilters]:checked').val() == 'all' ){
            $('.order-item.ship_pickup').show('normal');
        }
        else{
            //自提方式：
			var address = $('[name=pickupfilters]:checked').val();
            $('.order-item.ship_pickup').hide();
            $('.order-item.ship_pickup[data-pickup='+address+']').show('normal');
        }
	});

    function order_status(orderId,status){    	
        ajaxAction('/orders/set_status',{order_id:orderId,status:status},null,function(request){
            if(request.ret == 0) {
                showSuccessMessage(request.msg);
                window.location.reload();
            }
            else{
                showErrorMessage(request.msg);
            }
        });
        return false;
    }

    function ship_order(orderId){
        var code = $('#ship-code-'+orderId).val();
        var type = $('#ship-type-'+orderId).val();
        if(type == ""){
            $('#ship-type-'+orderId).focus();
            showErrorMessage('请选择快递类型');
            return false;
        }
        if(code == ""){
            $('#ship-code-'+orderId).focus();
            showErrorMessage('请输入快递号');
            return false;
        }
        ajaxAction('/orders/set_status',{order_id:orderId,ship_code:code,ship_type:type,status:2},null,function(request){
            if(request.ret == 0) {
                $('#ship-'+orderId).find('.btn-ship').hide();
                $('#order-stat-txt-'+orderId).html('已发货').attr('color','green');
                $('#ship-'+orderId).parent('.order-item:first').removeClass('paid').addClass('shipped');
                if(type != 200 ){
                    $('#order-status-'+orderId).before('<p class="mb10">快递：'+type+'，单号：'+code+'</p>');
                }
                else{
                    $('#order-status-'+orderId).before('<p class="mb10">订单已发货</p>')
				}

                showSuccessMessage(request.msg);
            }
            else{
                showErrorMessage(request.msg);
            }

        });
    }

    function batch_ship(btn){

        var orderids = [];
        $('input[name="orders[]"]').each(function(){
            if( $(this).prop('checked') ){
                orderids[orderids.length] = $(this).val();
            }
        });

        if( orderids.length == 0 ){
            showErrorMessage('请先勾选订单');
            return false;
        }

        $(btn).attr('disabled','disabled');
        ajaxAction('/orders/set_status',{order_id:orderids,status:2},null,function(request){
            $(btn).removeAttr('disabled');
            if(request.ret == 0) {
                showSuccessMessage(request.msg);
                window.location.reload();
            }
            else{
                showErrorMessage(request.msg);
            }

        });
    }

    var clipboard = new Clipboard('.btn-clip');

    clipboard.on('success', function(e) {
        //console.info('Action:', e.action);
        //console.info('Text:', e.text);
        showSuccessMessage("订单信息已复制，可粘贴使用了");
        e.clearSelection();
    });
</script>